package com.bamboo.tool.view.component.actions;

import com.bamboo.tool.view.component.dialog.RenameDialog;
import com.bamboo.tool.view.component.tree.*;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;

/**
 * @author EDZ
 */
public class RenameAction extends AnAction {

    private ApiTree apiTree;


    public RenameAction(ApiTree apiTree) {
        super("重命名", "重命名", AllIcons.Actions.Edit);
        this.apiTree = apiTree;
    }

    @Override
    public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
        Object lastPathComponent = apiTree.getSelectionPath().getLastPathComponent();
        RenameDialog renameDialog = new RenameDialog(apiTree);
        if (lastPathComponent instanceof MethodNode) {
            renameDialog.setTitle("重命名接口描述");
        }
        if (lastPathComponent instanceof ClassNode) {
            renameDialog.setTitle("重命名类描述");
        }
        if (lastPathComponent instanceof ProjectNode) {
            renameDialog.setTitle("重命名项目描述");
        }
        renameDialog.showAndGet();
    }

    @Override
    public void update(@NotNull AnActionEvent anActionEvent) {
        if (apiTree.getSelectionCount() == 1 && !(apiTree.getSelectionPaths()[0].getLastPathComponent() instanceof ModuleNode)) {
            anActionEvent.getPresentation().setEnabledAndVisible(true);
        } else {
            anActionEvent.getPresentation().setEnabledAndVisible(false);
        }

    }
}
